<!--
 * @Author: [kangjie]
 * @Date: 2022-04-20 14:58:13
 * @LastEditors: awada 10254500+awadaawada@user.noreply.gitee.com
 * @LastEditTime: 2022-10-15 20:25:42
 * @Description:
-->
<template>
  <div>
    <el-button type="primary" @click="toAdd">新增</el-button>
    <el-table :data="baseData" style="width: 100%">
      <el-table-column type="index" :index="1" label="序号" width="180" />
      <el-table-column prop="name" label="名称" width="180" />
      <el-table-column prop="val" label="值">
        <template slot-scope="scope">
          <div>
            <span v-if="scope.row.val.indexOf('http://') === -1">{{ scope.row.val }}</span>
            <img
              v-else
              width="50px"
              :src="scope.row.val"
              alt=""
            >
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="introduce" label="介绍" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="toEdit(scope.row.id)">编辑</el-button>
          <el-button type="primary" @click="toDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--模态框 -->
    <el-dialog title="新增基础配置" :visible.sync="visible">
      <el-form :model="form">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="介绍" :label-width="formLabelWidth">
          <el-input v-model="form.introduce" autocomplete="off" />
        </el-form-item>
        <el-form-item label="类型" :label-width="formLabelWidth">
          <el-select v-model="type" placeholder="请选择活动区域">
            <el-option label="文字" value="文字" />
            <el-option label="图片" value="图片" />
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="type === '文字'"
          label="值"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.val" autocomplete="off" />
        </el-form-item>
        <el-form-item v-else label="封面" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="http://121.4.83.32:7001/UploadControlle/upload"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="form.val" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false;form={}">取 消</el-button>
        <el-button type="primary" @click="qd()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { get, post } from '@/utils/request'
import qs from 'qs'
export default {
  data() {
    return {
      baseData: [],
      form: {},
      type: '文字',
      visible: false,
      formLabelWidth: '120px',
      imageUrl: ''
    }
  },
  watch: {
    type() {
      this.form.val = ''
    }
  },
  created() {
    this.findAll()
  },
  methods: {
    async toDelete(id) {
      await get('/baseConfig/deleteById', id)
      this.findAll()
    },
    toEdit(row) {
      this.form = { ...row }
      this.title = '编辑基础配置'
      this.visible = true
    },
    toAdd() {
      this.title = '新增基础配置'
      this.visible = true
    },
    async qd() {
      await post('/baseConfig/saveOrUpdate', qs.stringify(this.form))
      this.findAll()
    },
    async findAll() {
      const res = await get('/baseConfig/findAll')
      console.log(res)
      this.baseData = res.data
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.form.val = res.data
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>
